# -*- coding: utf-8 -*-
# @Author : cm_Simple
# @Time : 2022/1/4
# @Remark: 水浸 view 层
from mxsoftpy import View
from mxsoftpy.exception import ParamsError
from index import _
from server.common.IOTManager.Water.water import (WaterCheckServer,
                                                  WaterDevicesServer,
                                                  WaterDataServer)


def check_params(request_params: dict, must_params: set, available_params: set = None) -> dict:
    """
    参数校验
    :param request_params: 请求参数
    :param must_params: 必填参数
    :param available_params: 可选参数
    :return: 参数
    """
    set_keys = set(request_params.keys())
    lack_params = must_params - set_keys
    if lack_params:
        raise ParamsError('获取必填参数%s失败' % lack_params)
    # 去除无效参数
    # XXX 没有找到 pydantic 验证方法， 暂时使用 map 来做
    if available_params:
        list(map(request_params.__delitem__, filter(lambda x: x not in available_params or not request_params[x],
                                                    set_keys)))
        # list(map(request_params.__delitem__, filter(request_params.__contains__, set_keys - available_params)))
    return request_params


class WaterCheckRead(View):
    def __init__(self, request):
        super().__init__(request)
        self.available_params = {'name', 'pageindex', 'pagesize', 'state',"code"}
        self.return_name = '水浸'
        self.server = WaterCheckServer()

    def get(self):
        """
        查看所有水浸设备
        """
        # 参数校验
        must_params = {'pageindex', 'pagesize'}
        request_params = check_params(self.request.GET, must_params, self.available_params)
        request_params['page_size'] = int(request_params.pop('pagesize', 10))
        request_params['page_index'] = int(request_params.pop('pageindex', 1))
        return _('查看%s设备数据成功' % self.return_name), self.server.read(**request_params)


class WaterDevicesRead(View):
    def __init__(self, request):
        super().__init__(request)
        self.return_name = '水浸'
        self.server = WaterDevicesServer()

    def get(self):
        """
        查看所有水浸设备
        """
        return _('查看%s设备成功' % self.return_name), self.server.read()


class WaterDataRead(View):
    def __init__(self, request):
        super().__init__(request)
        self.return_name = '水浸'
        self.server = WaterDataServer()

    def get(self):
        """
        查看水浸设备数据
        XXX 有个查询条件是通过设备名称搜索，设备名称不具备唯一性，目前做法 设备名模糊查询，将符合条件的设备都查出来
        """
        # 参数校验
        must_params = set()
        available_params = {'device_id', 'pageindex', 'pagesize', 'alert_time', 'state', 'device_name', 'start_time',
                            'end_time', 'contrast_type'}
        request_params = check_params(self.request.GET, must_params, available_params)
        keys = request_params.keys()
        if 'device_id' not in keys and 'device_name' not in keys:
            raise ParamsError(_('缺少可选参数中的必要参数 device_id 或 device_name'))
        request_params['status_type'] = int(request_params.get('state', 0))
        return _('查看%s设备数据成功' % self.return_name), self.server.read(**request_params)
